package com.uzai.console.web.pyq;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.common.dto.pyq.PyqComment;
import com.uzai.common.dto.pyq.PyqCommentDto;
import com.uzai.common.dto.pyq.PyqFollowSendDto;
import com.uzai.common.dto.pyq.PyqInfoDto;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.pyq.pyqfollowconfig.PyqFollowConfigDto;
import com.uzai.console.dto.pyq.pyqfollowconfig.PyqFollowConfigIdDto;
import com.uzai.console.dto.pyq.pyqfollowconfig.PyqFollowConfigQuery;
import com.uzai.console.dto.pyq.pyqfollowconfig.PyqFollowConfigStatusDto;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.Merchant;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.MerchantMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.pyq.PyqFollowConfigService;
import com.uzai.console.service.pyq.PyqFollowSendService;
import com.uzai.console.service.pyq.PyqFollowSendSyncService;
import com.uzai.console.vo.pyq.pyqfollowconfig.PyqFollowConfigVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.bind.util.JAXBSource;
import java.util.List;

/**
 * 朋友圈自动跟发发送
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "自动跟发朋友圈发送",tags = "个人微信|朋友圈|自动跟发朋友圈发送")
@ApiSort(31)
@RestController
public class PyqFollowSendController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(PyqFollowSendController.class);

    @Autowired
    private PyqFollowSendSyncService pyqFollowSendSyncService;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "被监控朋友圈发送（已废除）", notes = "被监控朋友圈发送（已废除）")
    @ApiOperationSupport(order = 5)
    @PostMapping("/pyq/followSend")
    public Object followSend(@RequestBody PyqFollowSendDto pyqFollowSendDto){

        if(pyqFollowSendDto.getMerId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商id不能为空");
        }

        Merchant merchant = merchantMapper.selectById(pyqFollowSendDto.getMerId());
        if(merchant == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        if(pyqFollowSendDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控机器人唯一id不能为空");
        }

        DeviceWechatInfo deviceWechatInfo = deviceWechatInfoMapper.selectById(pyqFollowSendDto.getDeviceUniqueId(), pyqFollowSendDto.getMerId());
        if(deviceWechatInfo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人不存在");
        }

        if(StringUtils.isBlank(pyqFollowSendDto.getSnsId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "朋友圈唯一id不能为空");
        }

        pyqFollowSendSyncService.followSendSync(pyqFollowSendDto);

        pyqFollowSendSyncService.deletePyqFollowSendSync();
        return true;
    }

    @ApiOperation(value = "跟发朋友圈发送内容", notes = "跟发朋友圈发送内容")
    @ApiOperationSupport(order = 5)
    @PostMapping("/pyq/followSendContent")
    public Object followSendContent(@RequestBody PyqInfoDto pyqInfoDto){

        logger.info("--朋友圈跟发发送内容--pyqInfoDto={}", JSONObject.toJSONString(pyqInfoDto));

        if(pyqInfoDto.getMerId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商id不能为空");
        }

        Merchant merchant = merchantMapper.selectById(pyqInfoDto.getMerId());
        if(merchant == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        if(pyqInfoDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器人唯一id不能为空");
        }

        if(pyqInfoDto.getDeviceType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器人类型不能为空");
        }

        DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getById(pyqInfoDto.getDeviceType());
        if(deviceTypeEnum == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器类型不存在");
        }

        Device device = deviceService.findDeviceInfoByDeviceUniqueId(pyqInfoDto.getDeviceUniqueId(), deviceTypeEnum.getKey(), pyqInfoDto.getMerId());
        if(device == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器人不存在");
        }

        if(StringUtils.isBlank(pyqInfoDto.getSnsId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "朋友圈id不能为空");
        }

        pyqFollowSendSyncService.followSendContentSync(pyqInfoDto);

        pyqFollowSendSyncService.deletePyqFollowSendSync();
        return true;
    }

    @ApiOperation(value = "跟发朋友圈发送评论", notes = "跟发朋友圈发送评论")
    @ApiOperationSupport(order = 5)
    @PostMapping("/pyq/followSendComment")
    public Object followSendComment(@RequestBody PyqCommentDto pyqCommentDto){

        logger.info("--朋友圈跟发发送评论--pyqCommentDto={}", JSONObject.toJSONString(pyqCommentDto));

        if(pyqCommentDto.getMerId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商id不能为空");
        }

        Merchant merchant = merchantMapper.selectById(pyqCommentDto.getMerId());
        if(merchant == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        if(pyqCommentDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控机器人唯一id不能为空");
        }

        DeviceWechatInfo deviceWechatInfo = deviceWechatInfoMapper.selectById(pyqCommentDto.getDeviceUniqueId(), pyqCommentDto.getMerId());
        if(deviceWechatInfo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人不存在");
        }

        if(pyqCommentDto.getList() == null || pyqCommentDto.getList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "评论列表为空");
        }

        List<PyqComment> commentList =  pyqCommentDto.getList();
        if(commentList == null || commentList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "评论不能为空");
        }

        for(PyqComment pyqComment : commentList){
            if(StringUtils.isBlank(pyqComment.getSnsId()) || StringUtils.isBlank(pyqComment.getCommentId()) ){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "朋友圈id和评论id不能为空");
            }
        }

        //自动发送朋友圈评论
        pyqFollowSendSyncService.followSendCommentSync(pyqCommentDto);

        //删除朋友圈评论
        pyqFollowSendSyncService.deletePyqFollowSendSync();
        return true;
    }


}
